/**
 * Copyright (C), 2019-2020,个人开发
 * 文件名: ZgbSsoTokenFilter1
 * 作者: lsyzx【zhux@zhengebang.com】
 * 创建时间: 2019/2/24 15:04
 */
package com.zx.sso.core.filter;

import com.zx.sso.core.conf.Conf;
import com.zx.sso.core.entity.ReturnT;
import com.zx.sso.core.login.SsoTokenLoginHelper;
import com.zx.sso.core.user.ZxSsoUser;
import com.zx.sso.core.util.StringUtils;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者: lsyzx【zhux@zhengebang.com】
 * 创建时间: 2019/2/24 15:04
 * 描述: APP单点登录过滤器
 * 历史修改:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
@Slf4j
public class ZxSsoTokenFilter extends HttpServlet implements Filter {

    private String ssoServer;
    private String logoutPath;
    private String excludedPaths;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

        ssoServer = filterConfig.getInitParameter(Conf.SSO_SERVER);
        logoutPath = filterConfig.getInitParameter(Conf.SSO_LOGOUT_PATH);
        excludedPaths = filterConfig.getInitParameter(Conf.SSO_EXCLUDED_PATHS);

        log.info("XxlSsoTokenFilter init.");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        HttpServletResponse res = (HttpServletResponse) response;
        // make url
        String servletPath = req.getServletPath();

        // excluded path check
        if (StringUtils.checkexcludedPaths(excludedPaths, servletPath)) {
            // excluded path, allow
            chain.doFilter(request, response);
            return;
        }
        // logout filter
        if (logoutPath != null
                && logoutPath.trim().length() > 0
                && logoutPath.equals(servletPath)) {

            // logout
            SsoTokenLoginHelper.logout(req);

            // response
            res.setStatus(HttpServletResponse.SC_OK);
            res.setContentType("application/json;charset=UTF-8");
            res.getWriter().println("{\"code\":" + ReturnT.SUCCESS_CODE + ", \"msg\":\"\"}");

            return;
        }

        // login filter
        ZxSsoUser xxlUser = SsoTokenLoginHelper.loginCheck(req);
        if (xxlUser == null) {

            // response
            res.setStatus(HttpServletResponse.SC_OK);
            res.setContentType("application/json;charset=UTF-8");
            res.getWriter().println("{\"code\":" + Conf.SSO_LOGIN_FAIL_RESULT.getCode() + ", \"msg\":\"" + Conf.SSO_LOGIN_FAIL_RESULT.getMsg() + "\"}");
            return;
        }

        // ser sso user
        request.setAttribute(Conf.SSO_USER, xxlUser);


        // already login, allow
        chain.doFilter(request, response);
        return;
    }
}
